package com.example.healing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.healing.common.lang.Result;
import com.example.healing.entity.Comment;
import com.example.healing.entity.ListSong;
import com.example.healing.mapper.CommentMapper;
import com.example.healing.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author neymar
 * @date 2021年11月30日 15:33
 */
@Service
public class CommentImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * @author neymar
     * @date 2021/11/30 15:53
     * @param comment
     * @return int
     * @description 新增评论
     */
    @Override
    public Result create(Comment comment) {
        // 生成uuid 主键
        String cid = UUID.randomUUID().toString().replace("-", "");
        // 评论日期
        Date date = new Date();
        System.out.println(date);
        comment.setCid(cid);
        comment.setDate(date);
        int res =  commentMapper.insert(comment);
        return res == -1 ? Result.fail("评论发送失败") : Result.succ("评论发送成功");
    }

    /**
     * @author neymar
     * @date 2021/12/1 11:19
     * @param cid
     * @return com.example.healing.common.lang.Result
     * @description 删除评论
     */
    @Override
    public Result delete(String cid) {
        int res = commentMapper.deleteById(cid);
        return res == -1 ? Result.fail("删除失败") : Result.succ("删除成功");
    }

    /**
     * @author neymar
     * @date 2021/12/1 22:52
     * @param songId
     * @return com.example.healing.common.lang.Result
     * @description 获取歌曲评论
     */
    @Override
    public Result getComment(String songId) {
//        QueryWrapper<Comment> commentsQuery = new QueryWrapper<Comment>().eq("songId",songId);
//        List<Comment> lists = commentMapper.selectList(commentsQuery);
        List<Map<String, String>> commens = commentMapper.getComment(songId);
        return Result.succ(commens);
    }

    /**
     * @author neymar
     * @date 2021/12/1 22:53
     * @param songId
     * @param uid
     * @return com.example.healing.common.lang.Result
     * @description 获取用户的评论
     */
    @Override
    public Result getUserComment( String songId,String uid) {
        QueryWrapper<Comment> commentsQuery = new QueryWrapper<Comment>().eq("songId",songId).eq("uid",uid);
        List<Comment> lists = commentMapper.selectList(commentsQuery);
        int res = lists.size();
        return res > 0 ? Result.succ(lists) : Result.succ("您暂未对该歌曲作出评论~");
    }
}
